package com.zimmur.app.mocolink.manage.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.model.CommonQuestion;
import com.zimmur.app.mocolink.domain.model.UserGrade;
import com.zimmur.app.mocolink.domain.util.DateUtils;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.IUserGradeService;

@Controller
@RequestMapping("/userGrade")
public class UserGradeController extends BaseController{

	@Autowired
	private IUserGradeService userGradeService;
	
	@RequestMapping("/index")
	public String index(ModelMap model) {
		List<UserGrade> list = userGradeService.selectAll();
		model.addAttribute("gradeList", list);
		return "/center/userGrade/list";
	}
	@RequestMapping("/add")
	public String add(ModelMap model) {
		model.addAttribute("userGrade", new UserGrade());
		return "/center/userGrade/edit";
	}
	@RequestMapping("/edit")
	public String edit(ModelMap model) {
		Integer gradeId = getParam("gradeId", 0);
		UserGrade grade = userGradeService.selectById(gradeId);
		model.addAttribute("userGrade", grade);
		return "/center/userGrade/edit";
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public ReturnJsonEntity save(UserGrade userGrade) {
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		int flag = 0;
		if (userGrade.getId() != null && userGrade.getId() > 0) {
			userGrade.setCreator(getUsername());
			userGrade.setCreateTime(DateUtils.getNow());
			flag =userGradeService.update(userGrade);
		} else {
			userGrade.setId(null);
			userGrade.setCreator(getUsername());
			userGrade.setCreateTime(DateUtils.getNow());
			flag = userGradeService.add(userGrade);
		}
		if (flag > 0) {
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/delete")
	public ReturnJsonEntity delete() {
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer gradeId = getParam("gradeId", 0);
		int flag = userGradeService.delete(gradeId);
		if (flag > 0) {
			codeEnum = StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum = StatusCodeEnum.CODE600401;
		}
		entity.init(codeEnum);
		return entity;
	}
}
